package v1

import (
	"net/http"
	"strconv"
	"hytx_media/pkg/app"
	"hytx_media/pkg/e"

	"github.com/gin-gonic/gin"
	"github.com/astaxie/beego/validation"
	"hytx_media/models"
	"time"
	"github.com/Unknwon/com"
)

type registerPersonal struct {
	MediaUserId      int    `valid:"Required"`
	Account          string `valid:"Required; MaxSize(20)"`
	Introduce        string `valid:"Required; MaxSize(50)"`
	Avatar           string `valid:"Required"`
	OperatorsName    string `valid:"Required; MaxSize(255)"`
	OperatorsIdNo    string `valid:"Required; MaxSize(19)"`
	OperatorsIdPhoto string `valid:"Required"`
	Address          int    `valid:"Required"`
	Email            string `valid:"Required; MaxSize(50)"`
}

// @Summary 注册-选择主题-个人

func RegisterPersonal(c *gin.Context) {
	appG := app.Gin{C: c}
	valid := validation.Validation{}

	account := c.PostForm("account")
	introduce := c.PostForm("introduce")
	avatar := c.PostForm("avatar")
	operators_name := c.PostForm("operators_name")
	operators_id_no := c.PostForm("operators_id_no")
	operators_id_photo := c.PostForm("operators_id_photo")
	address := c.PostForm("address")
	addressInt, _ := strconv.Atoi(address)
	email := c.PostForm("email")

	user, err := c.Get("user")

	if err == false {
		appG.ResponseMsg(http.StatusOK, "token 验证都失败了，哥", false)
		return
	}

	// 用户id
	id := user.(*models.MediaUser).ID

	v := registerPersonal{
		MediaUserId:      id,
		Account:          account,
		Introduce:        introduce,
		Avatar:           avatar,
		OperatorsName:    operators_name,
		OperatorsIdNo:    operators_id_no,
		OperatorsIdPhoto: operators_id_photo,
		Address:          addressInt,
		Email:            email,
	}

	ok, _ := valid.Valid(&v)

	if !ok {
		//fmt.Println(v)
		//fmt.Println(valid.Errors)
		appG.Response(http.StatusOK, e.INVALID_PARAMS, false)
		return
	}

	/*	// 注册Add
		media_user_info_service := media_user_info_service.MediaUserInfo{
			MediaUserId:      user.(*models.MediaUser).ID,
			Account:          account,
			Introduce:        introduce,
			Avatar:           avatar,
			OperatorsName:    operators_name,
			OperatorsIdNo:    operators_id_no,
			OperatorsIdPhoto: operators_id_photo,
			Address:          addressInt,
			Email:            email,
		}
		if err := media_user_info_service.Add(); err != nil {
			appG.ResponseMsg(http.StatusOK, "注册失败", false)
			return
		}

		// 修改身份-个人
		if err := models.ModifyMediaUserIdentity(1, id); err != nil {
			appG.ResponseMsg(http.StatusOK, "个人身份修改失败", false)
			return
		}*/

	db := models.DB.Begin()

	mediaUserInfo := models.MediaUserInfo{
		MediaUserId:      user.(*models.MediaUser).ID,
		Account:          account,
		Introduce:        introduce,
		Avatar:           avatar,
		OperatorsName:    operators_name,
		OperatorsIdNo:    operators_id_no,
		OperatorsIdPhoto: operators_id_photo,
		Address:          addressInt,
		Email:            email,
		CreatedAt:        time.Now(),
		UpdatedAt:        time.Now(),
	}
	// 注册Add
	if err := db.Create(&mediaUserInfo).Error; err != nil {
		db.Rollback()
		appG.ResponseMsg(http.StatusOK, "注册失败", false)
		return
	}

	if err := db.Model(&models.MediaUser{}).Where("id = ?", id).Update("identity", 1).Error; err != nil {
		db.Rollback()
		appG.ResponseMsg(http.StatusOK, "个人身份修改失败", false)
		return
	}
	db.Commit()

	appG.ResponseMsg(http.StatusOK, "ok", true)

}

type registerNewMedia struct {
	registerPersonal
	OtherAptitude           string `valid:"Required"`
	AuxData                 string `valid:"Required"`
	InstitutionName         string `valid:"Required"`
	InstitutionLicense      string `valid:"Required"`
	InstitutionConfirmation string `valid:"Required"`
	InstitutionWebSite      string `valid:"Required"`
	InstitutionAddress      string `valid:"Required"`
	IdentityType            int `valid:"Required"`  //身份类型
}

// 注册新闻媒体
func RegisterNewMedia(c *gin.Context) {
	appG := app.Gin{C: c}
	valid := validation.Validation{}

	account := c.PostForm("account")
	introduce := c.PostForm("introduce")
	avatar := c.PostForm("avatar")
	operators_name := c.PostForm("operators_name")
	operators_id_no := c.PostForm("operators_id_no")
	operators_id_photo := c.PostForm("operators_id_photo")
	address := c.PostForm("address")
	addressInt, _ := strconv.Atoi(address)
	email := c.PostForm("email")

	other_aptitude := c.PostForm("other_aptitude")
	aux_data := c.PostForm("aux_data")
	institution_name := c.PostForm("institution_name")
	institution_license := c.PostForm("institution_license")
	institution_confirmation := c.PostForm("institution_confirmation")
	institution_web_site := c.PostForm("institution_web_site")
	institution_address := c.PostForm("institution_address")

	//identity_type := c.PostForm("identity_type")
	identity_type := com.StrTo(c.PostForm("identity_type")).MustInt()

	user, err := c.Get("user")

	if err == false {
		appG.ResponseMsg(http.StatusOK, "token 验证都失败了，哥", false)
		return
	}

	// 用户id
	id := user.(*models.MediaUser).ID

	v := registerNewMedia{}

	v.MediaUserId = id
	v.Account = account
	v.Introduce = introduce
	v.Avatar = avatar
	v.OperatorsName = operators_name
	v.OperatorsIdNo = operators_id_no
	v.OperatorsIdPhoto = operators_id_photo
	v.Address = addressInt
	v.Email = email

	v.OtherAptitude = other_aptitude
	v.AuxData = aux_data
	v.InstitutionName = institution_name
	v.InstitutionLicense = institution_license
	v.InstitutionConfirmation = institution_confirmation
	v.InstitutionWebSite = institution_web_site
	v.InstitutionAddress = institution_address
	v.IdentityType = identity_type

	ok, _ := valid.Valid(&v)

	if !ok {
		appG.Response(http.StatusOK, e.INVALID_PARAMS, false)
		return
	}

	db := models.DB.Begin()

	mediaUserInfo := models.MediaUserInfo{
		MediaUserId:      user.(*models.MediaUser).ID,
		Account:          account,
		Introduce:        introduce,
		Avatar:           avatar,
		OperatorsName:    operators_name,
		OperatorsIdNo:    operators_id_no,
		OperatorsIdPhoto: operators_id_photo,
		Address:          addressInt,
		Email:            email,
		CreatedAt:        time.Now(),
		UpdatedAt:        time.Now(),
	}
	// 注册Add
	if err := db.Create(&mediaUserInfo).Error; err != nil {
		db.Rollback()
		appG.ResponseMsg(http.StatusOK, "注册失败", false)
		return
	}

	mediaUserInstitution := models.MediaUserInstitution{
		MediaUserId:             id,
		OtherAptitude:           other_aptitude,
		AuxData:                 aux_data,
		InstitutionName:         institution_name,
		InstitutionLicense:      institution_license,
		InstitutionConfirmation: institution_confirmation,
		InstitutionWebSite:      institution_web_site,
		InstitutionAddress:      institution_address,
		CreatedAt:               time.Now(),
		UpdatedAt:               time.Now(),
	}

	if err := db.Create(&mediaUserInstitution).Error; err != nil {
		db.Rollback()
		appG.ResponseMsg(http.StatusOK, "新闻媒体注册失败", false)
		return
	}


	if err := db.Model(&models.MediaUser{}).Where("id = ?", id).Update("identity", identity_type).Error; err != nil {
		db.Rollback()
		appG.ResponseMsg(http.StatusOK, "个人身份修改失败", false)
		return
	}
	db.Commit()

	appG.ResponseMsg(http.StatusOK, "ok", true)
}

type updatePersonal struct {
	Account          string `valid:"Required; MaxSize(20)"`
	Introduce        string `valid:"Required; MaxSize(50)"`
	Avatar           string `valid:"Required"`
	OperatorsName    string `valid:"Required; MaxSize(255)"`
	OperatorsIdNo    string `valid:"Required; MaxSize(19)"`
	OperatorsIdPhoto string `valid:"Required"`
	Address          int    `valid:"Required"`
	Email            string `valid:"Required; MaxSize(50)"`
}

type updateNewMedia struct {
	updatePersonal
	OtherAptitude           string `valid:"Required"`
	AuxData                 string `valid:"Required"`
	InstitutionName         string `valid:"Required"`
	InstitutionLicense      string `valid:"Required"`
	InstitutionConfirmation string `valid:"Required"`
	InstitutionWebSite      string `valid:"Required"`
	InstitutionAddress      string `valid:"Required"`
	IdentityType            int `valid:"Required"`  //身份类型
}

func RegisterPersonalList(c *gin.Context)  {
	appG := app.Gin{C: c}
	user, _ := c.Get("user")
	// 用户id
	id := user.(*models.MediaUser).ID

	var media_user_info = models.MediaUserInfo{}

	err := models.DB.Table("media_user_info").Where("media_user_id = ?", id).Find(&media_user_info).Error
	if err != nil {
		appG.ResponseMsg(http.StatusOK, "错误", false)
		return
	}

	var media_user_institution = models.MediaUserInstitution{}
	models.DB.Table("media_user_institution").Where("media_user_id = ?", id).Find(&media_user_institution)

	var a []interface{}

	a = append(a, media_user_institution, media_user_info)

	appG.ResponseMsg(http.StatusOK, "ok", a)
}


// @Summary 修改-选择主题-个人

func RegisterPersonalUpdate(c *gin.Context) {
	appG := app.Gin{C: c}
	valid := validation.Validation{}

	account := c.PostForm("account")
	introduce := c.PostForm("introduce")
	avatar := c.PostForm("avatar")
	operators_name := c.PostForm("operators_name")
	operators_id_no := c.PostForm("operators_id_no")
	operators_id_photo := c.PostForm("operators_id_photo")
	address := c.PostForm("address")
	addressInt, _ := strconv.Atoi(address)
	email := c.PostForm("email")

	user, err := c.Get("user")

	if err == false {
		appG.ResponseMsg(http.StatusOK, "token 验证都失败了，哥", false)
		return
	}

	// 用户id
	id := user.(*models.MediaUser).ID

	v := updatePersonal{
		Account:          account,
		Introduce:        introduce,
		Avatar:           avatar,
		OperatorsName:    operators_name,
		OperatorsIdNo:    operators_id_no,
		OperatorsIdPhoto: operators_id_photo,
		Address:          addressInt,
		Email:            email,
	}

	ok, _ := valid.Valid(&v)

	if !ok {
		//fmt.Println(valid.Errors)
		appG.Response(http.StatusOK, e.INVALID_PARAMS, false)
		return
	}

	db := models.DB.Begin()

	mediaUserInfo := models.MediaUserInfo{
		Account:          account,
		Introduce:        introduce,
		Avatar:           avatar,
		OperatorsName:    operators_name,
		OperatorsIdNo:    operators_id_no,
		OperatorsIdPhoto: operators_id_photo,
		Address:          addressInt,
		Email:            email,
		CreatedAt:        time.Now(),
		UpdatedAt:        time.Now(),
	}
	// 注册Add
	if err := db.Model(&models.MediaUserInfo{}).Where("media_user_id = ?", id).Update(&mediaUserInfo).Error; err != nil {
		db.Rollback()
		appG.ResponseMsg(http.StatusOK, "修改失败", false)
		return
	}

	if err := db.Model(&models.MediaUser{}).Where("id = ?", id).Update("status", 1).Error; err != nil {
		db.Rollback()
		appG.ResponseMsg(http.StatusOK, "个人待审核修改失败", false)
		return
	}
	db.Commit()

	appG.ResponseMsg(http.StatusOK, "ok", true)

}

// 修改新闻媒体
func RegisterNewMediaUpdate(c *gin.Context) {
	appG := app.Gin{C: c}
	valid := validation.Validation{}

	account := c.PostForm("account")
	introduce := c.PostForm("introduce")
	avatar := c.PostForm("avatar")
	operators_name := c.PostForm("operators_name")
	operators_id_no := c.PostForm("operators_id_no")
	operators_id_photo := c.PostForm("operators_id_photo")
	address := c.PostForm("address")
	addressInt, _ := strconv.Atoi(address)
	email := c.PostForm("email")

	other_aptitude := c.PostForm("other_aptitude")
	aux_data := c.PostForm("aux_data")
	institution_name := c.PostForm("institution_name")
	institution_license := c.PostForm("institution_license")
	institution_confirmation := c.PostForm("institution_confirmation")
	institution_web_site := c.PostForm("institution_web_site")
	institution_address := c.PostForm("institution_address")

	//identity_type := c.PostForm("identity_type")
	identity_type := com.StrTo(c.PostForm("identity_type")).MustInt()

	user, err := c.Get("user")

	if err == false {
		appG.ResponseMsg(http.StatusOK, "token 验证都失败了，哥", false)
		return
	}

	// 用户id
	id := user.(*models.MediaUser).ID

	v := updateNewMedia{}

	v.Account = account
	v.Introduce = introduce
	v.Avatar = avatar
	v.OperatorsName = operators_name
	v.OperatorsIdNo = operators_id_no
	v.OperatorsIdPhoto = operators_id_photo
	v.Address = addressInt
	v.Email = email

	v.OtherAptitude = other_aptitude
	v.AuxData = aux_data
	v.InstitutionName = institution_name
	v.InstitutionLicense = institution_license
	v.InstitutionConfirmation = institution_confirmation
	v.InstitutionWebSite = institution_web_site
	v.InstitutionAddress = institution_address
	v.IdentityType = identity_type

	ok, _ := valid.Valid(&v)

	if !ok {
		appG.Response(http.StatusOK, e.INVALID_PARAMS, false)
		return
	}

	db := models.DB.Begin()

	mediaUserInfo := models.MediaUserInfo{
		Account:          account,
		Introduce:        introduce,
		Avatar:           avatar,
		OperatorsName:    operators_name,
		OperatorsIdNo:    operators_id_no,
		OperatorsIdPhoto: operators_id_photo,
		Address:          addressInt,
		Email:            email,
		CreatedAt:        time.Now(),
		UpdatedAt:        time.Now(),
	}
	// 注册Add
	if err := db.Model(&models.MediaUserInfo{}).Where("media_user_id = ?", id).Update(&mediaUserInfo).Error; err != nil {
		db.Rollback()
		appG.ResponseMsg(http.StatusOK, "注册失败", false)
		return
	}

	mediaUserInstitution := models.MediaUserInstitution{
		OtherAptitude:           other_aptitude,
		AuxData:                 aux_data,
		InstitutionName:         institution_name,
		InstitutionLicense:      institution_license,
		InstitutionConfirmation: institution_confirmation,
		InstitutionWebSite:      institution_web_site,
		InstitutionAddress:      institution_address,
		CreatedAt:               time.Now(),
		UpdatedAt:               time.Now(),
	}

	if err := db.Model(&models.MediaUserInstitution{}).Where("media_user_id = ?", id).Update(&mediaUserInstitution).Error; err != nil {
		db.Rollback()
		appG.ResponseMsg(http.StatusOK, "新闻媒体注册失败", false)
		return
	}


	if err := db.Model(&models.MediaUser{}).Where("id = ?", id).Update("status", 1).Error; err != nil {
		db.Rollback()
		appG.ResponseMsg(http.StatusOK, "个人身份修改失败", false)
		return
	}
	db.Commit()

	appG.ResponseMsg(http.StatusOK, "ok", true)
}